package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swimv2.ejb.entity.Capacita;
import swimv2.ejb.entity.CapacitaRichiesta;
import swimv2.ejb.remote.GestioneCapacitaRemote;
import util.HtmlUtil;


public class AdminCapacita extends Servlet {
	private static final long serialVersionUID = 1L;
    
    public AdminCapacita() {
        super();

		
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	    List<String> elencoCapacita = getElencoNomiCapacita();
	    request.setAttribute("elencoCapacita", elencoCapacita);
	    
		try {
        	Context jndiContext;
			jndiContext = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneCapacita/remote");
			GestioneCapacitaRemote gestioneCapacitaRemote =(GestioneCapacitaRemote) PortableRemoteObject.narrow(ref1, GestioneCapacitaRemote.class);
			List<CapacitaRichiesta> capacitaRichieste = gestioneCapacitaRemote.elencoRichiesteCapacita();
			request.setAttribute("capacitaRichieste", capacitaRichieste);
		
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	    
	    
	    HtmlUtil.inviaHeader(request, response);
	    HtmlUtil.inviaIntestazioneUtente(request, response);
	    HtmlUtil.includi(request, response, "/WEB-INF/include/admin/listaCapacita.jsp");
	    HtmlUtil.inviaFooter(request, response)	 ;   
	    
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String azione=(String) request.getParameter("azione");
		if(azione.equals("aggiungiCapacita"))
		{
			String capacita=(String) request.getParameter("capacita");
			aggiungiCapacita(capacita);
			HtmlUtil.redirect(request, response, "/AdminCapacita");
			return;
		} else if (azione.equals("confermaCapacita"))
		{
			confermaCapacita(request,response);
			
			
		} else if (azione.equals("rifiutaCapacita"))
		{
			rifiutaCapacita(request,response);
		}
	}
	
	private void rifiutaCapacita(HttpServletRequest request,
			HttpServletResponse response) {
		int id=Integer.parseInt(request.getParameter("id"));
		try {
        	Context jndiContext;
			jndiContext = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneCapacita/remote");
			GestioneCapacitaRemote gestioneCapacitaRemote =(GestioneCapacitaRemote) PortableRemoteObject.narrow(ref1, GestioneCapacitaRemote.class);
			gestioneCapacitaRemote.rifiutaCapacita(id);
			HtmlUtil.redirect(request, response, "/AdminCapacita");
		
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}


	private void confermaCapacita(HttpServletRequest request,
			HttpServletResponse response) {
		int id=Integer.parseInt(request.getParameter("id"));
		try {
        	Context jndiContext;
			jndiContext = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneCapacita/remote");
			GestioneCapacitaRemote gestioneCapacitaRemote =(GestioneCapacitaRemote) PortableRemoteObject.narrow(ref1, GestioneCapacitaRemote.class);
			gestioneCapacitaRemote.confermaCapacita(id);
			HtmlUtil.redirect(request, response, "/AdminCapacita");
		
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}


	private void aggiungiCapacita(String capacita) {
		try {
        	Context jndiContext;
			jndiContext = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneCapacita/remote");
			GestioneCapacitaRemote gestioneCapacitaRemote =(GestioneCapacitaRemote) PortableRemoteObject.narrow(ref1, GestioneCapacitaRemote.class);
			gestioneCapacitaRemote.registraCapacita(capacita);
	
		
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}


	protected List<String> getElencoNomiCapacita(){
		try {
        	Context jndiContext;
			jndiContext = new javax.naming.InitialContext();
			Object ref1 = jndiContext.lookup("GestioneCapacita/remote");
			GestioneCapacitaRemote gestioneCapacitaRemote =(GestioneCapacitaRemote) PortableRemoteObject.narrow(ref1, GestioneCapacitaRemote.class);
			List<Capacita> listaCapacita = gestioneCapacitaRemote.getListaCapacita();
			
			List<String> listaNomiCapacita = new ArrayList<String>();
			for(Capacita capacita:listaCapacita) {
				if(capacita != null)
				listaNomiCapacita.add(capacita.getDescrizione());
			}
			return listaNomiCapacita;
		
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	
	
}
